// Scripts/Core/GameManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum GameMode
{
    SwapMode,        // 原有的交换消除模式
    ClickMode        // 新的点击消除模式
}

public class GameManager : MonoBehaviour
{
    public static GameManager Instance;
    
    [Header("游戏设置")]
    public int gridWidth = 8;
    public int gridHeight = 8;
    public int targetScore = 1000;
    public int maxMoves = 30;
    public GameMode currentGameMode = GameMode.SwapMode;
    
    [Header("引用")]
    public GridManager gridManager;
    public GameCanvas gameCanvas;
    public TilePool tilePool;
    public ClickEliminationMode clickEliminationMode;
    
    // 游戏状态
    private int currentScore = 0;
    private int movesLeft = 0;
    private int currentLevel = 1;
    private bool isGameActive = true;
    
    void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
            DontDestroyOnLoad(gameObject);
        }
        else
        {
            Destroy(gameObject);
        }
    }
    
    void Start()
    {
        InitializeGame();
    }
    
    void InitializeGame()
    {
        movesLeft = maxMoves;
        currentScore = 0;
        
        gameCanvas.UpdateScore(currentScore);
        gameCanvas.UpdateMoves(movesLeft);
        
        gridManager.InitializeGrid(gridWidth, gridHeight);
        
        // 初始化游戏模式
        InitializeGameMode();
    }
    
    void InitializeGameMode()
    {
        // 确保点击消除模式组件存在
        if (clickEliminationMode == null)
        {
            clickEliminationMode = FindObjectOfType<ClickEliminationMode>();
            if (clickEliminationMode == null)
            {
                // 如果没有找到，创建一个
                GameObject clickModeObj = new GameObject("ClickEliminationMode");
                clickModeObj.transform.SetParent(transform);
                clickEliminationMode = clickModeObj.AddComponent<ClickEliminationMode>();
                clickEliminationMode.gridManager = gridManager;
            }
        }
        
        // 根据当前模式启用相应的组件
        SwitchGameMode(currentGameMode);
    }
    
    public void AddScore(int points)
    {
        currentScore += points;
        gameCanvas.UpdateScore(currentScore);
        
        if (currentScore >= targetScore)
        {
            LevelComplete();
        }
    }
    
    public void UseMove()
    {
        movesLeft--;
        gameCanvas.UpdateMoves(movesLeft);
        
        if (movesLeft <= 0)
        {
            GameOver();
        }
    }
    
    void LevelComplete()
    {
        isGameActive = false;
        // 显示通关界面
        Debug.Log("关卡完成！");
    }
    
    void GameOver()
    {
        isGameActive = false;
        // 显示游戏结束界面
        Debug.Log("游戏结束");
    }
    
    public bool CanMakeMove()
    {
        return isGameActive && movesLeft > 0;
    }
    
    /// <summary>
    /// 切换游戏模式
    /// </summary>
    /// <param name="newMode">新的游戏模式</param>
    public void SwitchGameMode(GameMode newMode)
    {
        currentGameMode = newMode;
        
        switch (currentGameMode)
        {
            case GameMode.SwapMode:
                // 启用交换模式，禁用点击模式
                gridManager.enabled = true;
                if (clickEliminationMode != null)
                    clickEliminationMode.DisableMode();
                Debug.Log("切换到交换消除模式");
                break;
                
            case GameMode.ClickMode:
                // 启用点击模式，禁用交换模式的输入处理
                gridManager.enabled = false; // 禁用GridManager的输入处理
                if (clickEliminationMode != null)
                    clickEliminationMode.EnableMode();
                Debug.Log("切换到点击消除模式");
                break;
        }
    }
    
    /// <summary>
    /// 切换到交换消除模式
    /// </summary>
    public void SwitchToSwapMode()
    {
        SwitchGameMode(GameMode.SwapMode);
    }
    
    /// <summary>
    /// 切换到点击消除模式
    /// </summary>
    public void SwitchToClickMode()
    {
        SwitchGameMode(GameMode.ClickMode);
    }
    
    /// <summary>
    /// 获取当前游戏模式
    /// </summary>
    public GameMode GetCurrentGameMode()
    {
        return currentGameMode;
    }
}